package com.kotlin.book.activity

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.kotlin.book.adapter.BookItemColumn4Adapter
import com.kotlin.book.adapter.DetailCommentItemAdapter
import com.kotlin.book.api.BookApi
import com.kotlin.book.api.CommentApi
import com.kotlin.book.databinding.ActivityDetailBinding
import com.kotlin.book.util.Util


class DetailActivity : AppCompatActivity() {
    private lateinit var binding: ActivityDetailBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        //根据系统ui设置边距
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            insets
        }
        // 参数接收
        val bundle = this.intent.extras
        val id: Int = bundle?.getInt("id")!!

        //返回
        binding.black.setOnClickListener {
            finish()
        }
        binding.beganRead.setOnClickListener{
            // 页面跳转
            val intent = Intent(this, ReadActivity::class.java)
            val bundle1 = Bundle()
            // 传参
            bundle1.putInt("bookId", id)
            intent.putExtras(bundle1)
            this.startActivity(intent)
        }
        //滚动事件监听
        binding.scroll.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            val alpha = (scrollY / 200f).coerceIn(0f, 1f)
            // 动态调整透明度
            binding.defaultNavbar.alpha = 1 - alpha
            binding.activeNavbar.alpha = alpha
        }

        //详情
        BookApi.getBookDetail(mapOf("id" to id)) { response ->
            response?.data?.let {
                it.author?.let{ author->
                    getBookListByAuthor(author)
                }
                getLoveList(it.typeId)
                runOnUiThread {
                    Util.getImage(
                        it.pic,
                        binding.bookImage,
                        this
                    )
                    Util.getImage(
                        it.pic,
                        binding.headerBg,
                        this
                    )

                    binding.bookName.text = it.name
                    binding.authorName.text = it.author
                    binding.bookScore.text = it.source.toString()
                    binding.bookTag.text = it.tag?.replace(",", "-")
                    val word = it.word?.toInt().toString() + "万字"
                    binding.text.text = "${it.type ?: ""}  |  ${word}  |  ${it.state}"
                    binding.oneChapterName.text = it.oneChapterName ?: ""
                    binding.updateTime.text = it.updateTime
                    binding.collectionCount.text = it.collectionCount?.run {
                        if (this != 0 && this > 10000) {
                            String.format("%.1f万", (this / 10000.0))
                        } else {
                            this.toString()
                        }
                    }
                    binding.readCount.text = it.readCount?.run {
                        if (this != 0 && this > 10000) {
                            String.format("%.1f万", (this / 10000.0))
                        } else {
                            this.toString()
                        }
                    }
                    binding.commentCount.text = it.commentCount?.run {
                        if (this != 0 && this > 10000) {
                            String.format("%.1f万", (this / 10000.0))
                        } else {
                            this.toString()
                        }
                    }
                    binding.commentCount1.text = it.commentCount?.toString()
                }
            }
        }
        getCommentList(id)
    }

    //评论
    private fun getCommentList(id: Int) {
        binding.commentBox.layoutManager = LinearLayoutManager(this)
        CommentApi.getCommentList(mapOf("bookId" to id)) { response ->
            response?.data?.let {
                runOnUiThread {
                    binding.commentBox.adapter = DetailCommentItemAdapter(it)
                }
            }
        }
    }

    //作者作品
    private fun getBookListByAuthor(author: String) {
        binding.detailAuthorList.layoutManager = GridLayoutManager(this, 4)
        BookApi.getBookListByAuthor(mapOf("author" to author)) { response ->
            response?.data?.let {
                runOnUiThread {
                    binding.detailAuthorList.adapter =
                        BookItemColumn4Adapter(mapOf("authorName" to false), it)
                }
            }
        }
    }

    //猜你喜欢
    private fun getLoveList(typeId:Int) {
        binding.detailLoveList.layoutManager = GridLayoutManager(this, 4)
        BookApi.getRandBookList(mapOf("typeId" to typeId,"pageSize" to 4)) { response ->
            response?.data?.let {
                runOnUiThread {
                    binding.detailLoveList.adapter =
                        BookItemColumn4Adapter(mapOf("authorName" to true), it)
                }
            }
        }
    }
}